/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws. 
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIESREGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT.  ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability 
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the 
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2015, 2016 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : r_cg_main.c
* Version      : Code Generator for RX23T V1.00.04.02 [29 Nov 2016]
* Device(s)    : R5F523T5AxFM
* Tool-Chain   : CCRX
* Description  : This file implements main function.
* Creation Date: 2017/8/2
***********************************************************************************************************************/

/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_icu.h"
#include "r_cg_port.h"
#include "r_cg_tmr.h"
#include "r_cg_rspi.h"
#include "r_cg_sci.h"
/* Start user code for include. Do not edit comment generated here */
#include "all.h"

/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */

/* End user code. Do not edit comment generated here */


static void R_MAIN_UserInit(void);
/***********************************************************************************************************************
* Function Name: main
* Description  : This function implements main function.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void main(void)
{
    R_MAIN_UserInit();
    /* Start user code. Do not edit comment generated here */
    while (1U)
    {


		Update_Key_Value();                 // 更新按键状态值 上电后按下按键，进入一键启动
		Update_Encoder_Value();             // 更新编码器当前值 编码器的值决定执行什么程控任务

		Msg_FMU_To_Ctrl_Update();           // 接收、解析飞控发送的信息
		Msg_Smp_To_Ctrl_Update();           // 接收、解析采集板发送的信息

		Debug_Message_Handle();             // 处理调试信息(命令)

		Unlock_FMU_By_Key_Handle();         // 解锁按键信息处理

		SysCtrl_Update_PID();               // 更新系统PID 设定值、采样值、误差、运算……
		//判断现在是哪个ACT，如果满足进入下一个ACT的条件，标记FeatureValid为NO
		//找起点FeatureValid为NO
		SysCtrl_Update_Act();               // 更新系统控制状态(动作)
		SysCtrl_Update_Lost_Info();         // 更新系统丢失特征点如何继续追踪信息
		SysCtrl_Update_Msg_Ctrl_To_FMU();   // 更新控制板发送给飞控的信息(控制命令)
		Dbg_U1_Send_Msg();                  // 串口1输出调试信息
    	Task_Run();
    }
    /* End user code. Do not edit comment generated here */
}
/***********************************************************************************************************************
* Function Name: R_MAIN_UserInit
* Description  : This function adds user code before implementing main function.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_MAIN_UserInit(void)
{
    /* Start user code. Do not edit comment generated here */

    PID_Parameter_Init();               // 系统PID参数初始化
    Dbg_PID_Cache_Init();               // PID调试缓存值初始化
    Usart_Rx_Buf_Init ();               // 串口接收缓存区初始化

    Update_Key_PowerOn_Value();         // 获取按键系统上电时的状态
    Update_Encoder_PowerOn_Value();     // 获取编码器在系统上电时的初始状态
    R_TMR0_Start();//全局定时器开始工作，延时函数
    SysDelay_ms(1000);                  // 延时等待系统稳定

    SysCtrl_Init_Find_Start_Point();    // 初始化找起点的数据记录

    R_ICU_IRQ2_Start();//采集板通讯中断
    R_SCI1_Start();//飞控通讯
	R_SCI5_Start();//调试通讯

    Usart_Receive_IT(USART5, U5RxBuf, sizeof(Msg_FMUToCtrl_t));  // 串口5准备接收准备接收飞控发送的数据包
    Usart_Receive_IT(USART1, U1RxBuf, 1);    // 调试

	Task_Add(Led_normal,500);



    /* End user code. Do not edit comment generated here */
}

/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
